<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="svm.resources.html">« 资源类型</a></li>
      <li style="float: right;"><a href="class.svm.html">SVM »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.svm.html">SVM</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="svm.examples" class="chapter">
 <h1>范例</h1>


 <p class="para">
  The basic process is to define parameters, supply training data to generate a 
  model on, then make predictions based on the model. There are a default set 
  of parameters that should get some results with most any input, so we&#039;ll start 
  by looking at the data. 
 </p>
 <p class="para">
  Data is supplied in either a file, a stream, or as an array. If supplied in 
  a file or a stream, it must contain one line per training example, which must 
  be formatted as an integer class (usually 1 and -1) followed by a series of 
  feature/value pairs, in increasing feature order. The features are integers, 
  the values floats, usually scaled 0-1. For example:
 </p>
 <p class="para">
  -1 1:0.43 3:0.12 9284:0.2
 </p>
 <p class="para">
  In a document classification problem, say a spam checker, each line would 
  represent a document. There would be two classes, -1 for spam, 1 for ham. 
  Each feature would represent some word, and the value would represent that 
  importance of that word to the document (perhaps the frequency count, with 
  the total scaled to unit length). Features that were 0 (e.g. the word did 
  not appear in the document at all) would simply not be included.
 </p>
 <p class="para">
  In array mode, the data must be passed as an array of arrays. Each sub-array 
  must have the class as the first element, then key =&gt; value sets for the 
  feature values pairs.
 </p>
 <p class="para">
  This data is passed to the SVM class&#039;s train function, which will return an 
  SVM model is successful.
 </p>
 <p class="para">
  Once a model has been generated, it can be used to make predictions about 
  previously unseen data. This can be passed as an array to the model&#039;s 
  predict function, in the same format as before, but without the label. 
  The response will be the class. 
 </p>
 <p class="para">
  Models can be saved and restored as required, using the save and load 
  functions, which both take a file location. 
 </p>
 <p class="para">
  <div class="example" id="example-4559">
   <p><strong>示例 #1 Train from array</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.43</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">9284&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.2</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.22</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.01</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">94&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.11</span><span style="color: #007700">),<br />);<br /><br /></span><span style="color: #0000BB">$svm&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SVM</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$svm</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">train</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.43</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">9284&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$model</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">predict</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$model</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">save</span><span style="color: #007700">(</span><span style="color: #DD0000">'model.svm'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(-1)
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4560">
   <p><strong>示例 #2 Train from a file</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$svm&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SVM</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$model&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$svm</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">train</span><span style="color: #007700">(</span><span style="color: #DD0000">"traindata.txt"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
</div>
</div></div></body></html>